<template>
  <div>
    <div class="chart-item" v-for="(item, index) in chartData" :key="index">
      <div class="title">
        {{ item.name }}
        <div class="tools">
          <el-button type="primary" plain size="small" @click="handleBaseLine(index)">柱状图</el-button>
          <el-button type="primary" plain size="small" @click="handleStackLine(index)">堆叠柱状图</el-button>
          <el-button type="primary" plain size="small" @click="handleBaseBar(index)">条形图</el-button>
          <el-button type="primary" plain size="small" @click="handleStackBar(index)">堆叠条形图</el-button>
        </div>
      </div>
      <div class="chart">
        <LineChart :chartOption="item.option" />
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted } from 'vue'
import LineChart from '../LineChart.vue'
import { chartOption } from '../chart'
const prop = defineProps({
  data: {
    type: String,
    default: ''
  }
})
const chartData = ref([])
onMounted(() => {
  const data = { ...prop.data }
  data.items.forEach((item) => {
    const totals = item.data.map((i) => {
      return i.reduce((a, b) => a + b, 0)
    })
    console.log(totals)
    const params = {
      name: item.name,
      chartTitle: `${data.name}和${item.name}的交叉图`,
      data: {
        xData: item.yOptions.map((i) => i.optionDesc),
        data: data.xOptions.map((i, index) => {
          return {
            name: i.optionDesc,
            data: item.data.map((j, iIndex) => {
              return ((j[index] / totals[iIndex]) * 100).toFixed(2)
            })
          }
        })
      }
    }
    params.option = chartOption(params.data, params.chartTitle, 'baseBar', {
      labelUnit: '%'
    })

    chartData.value.push(params)
  })
  console.log(chartData.value)
})
const handleStackLine = (index) => {
  chartData.value[index].option = chartOption(
    chartData.value[index].data,
    chartData.value[index].chartTitle,
    'stackBar',
    {
      labelUnit: '%'
    }
  )
}
const handleBaseLine = (index) => {
  chartData.value[index].option = chartOption(
    chartData.value[index].data,
    chartData.value[index].chartTitle,
    'baseBar',
    {
      labelUnit: '%'
    }
  )
}
const handleBaseBar = (index) => {
  chartData.value[index].option = chartOption(
    chartData.value[index].data,
    chartData.value[index].chartTitle,
    'horizontalBar',
    {
      labelUnit: '%'
    }
  )
}
const handleStackBar = (index) => {
  chartData.value[index].option = chartOption(
    chartData.value[index].data,
    chartData.value[index].chartTitle,
    'horizontalStackBar',
    {
      labelUnit: '%'
    }
  )
}
</script>

<style lang="scss" scoped>
.chart-item {
  padding-bottom: 10px;
  margin: 20px 0 40px 0;
  box-shadow: 0 0 10px #ccc;
  padding: 10px;
  border-radius: 5px;
}
.title {
  display: flex;
  justify-content: space-between;
  position: relative;
  padding: 20px;
  font-weight: 700;
  font-size: 20px;
  line-height: 30px;
  .tools {
    display: flex;
  }
  &::after {
    position: absolute;
    content: '';
    left: 0;
    top: 25px;
    height: 20px;
    width: 5px;
    background-color: #178aec;
  }
}
.chart {
  width: 100%;
}
</style>
